package mapper

import (
	"errors"
	"im/global"
	"im/model"
	"im/model/request"

	"gorm.io/gorm"
)

type UserContactMapper struct{}

// 添加好友
func AddContact(ucq request.UserContactReq) (model.UserContact, error) {
	var userContact = &model.UserContact{
		OwnerId:   ucq.OwnerId,
		ContactId: ucq.ContactId,
		Desc:      ucq.Desc,
	}
	return *userContact, global.DB.Create(userContact).Error
}

// 删除好友
func DelContact(ucq request.UserContactReq) (model.UserContact, error) {
	var userContact model.UserContact
	if errors.Is((global.DB.Where("owner_id = ? and contact_id = ?", ucq.OwnerId, ucq.ContactId).Find(&userContact).Error), gorm.ErrRecordNotFound) {
		return userContact, errors.New("删除失败，该好友不存在")
	}

	return userContact, global.DB.Delete(&userContact).Error
}

// 好友列表
func ListContacts(ucq request.UserContactReq) (interface{}, error) {
	var userContacts []model.UserContact
	return userContacts, global.DB.Model(&model.UserContact{}).Where("owner_id = ? ", ucq.OwnerId).Find(&userContacts).Error
}
